//
//  BaseSubViewCtrl.m
//  DogeLegs
//
//  Created by 宋华 on 2017/8/15.
//  Copyright © 2017年 宋华. All rights reserved.
//

#import "BaseSubViewCtrl.h"
#import "BaseRootViewCtrl.h"

@interface BaseSubViewCtrl () {

    UIButton *_btnRight;
    UIButton *_btnLeft;
}

@end

@implementation BaseSubViewCtrl

#pragma mark - Cycle Life
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        if (![self isKindOfClass:[BaseRootViewCtrl class]]) {
            self.hidesBottomBarWhenPushed = YES;
        }
    }
    return self;
}

- (id)init {
    if (self = [super init]) {
        if (![self isKindOfClass:[BaseRootViewCtrl class]]) {
            self.hidesBottomBarWhenPushed = YES;
        }
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)addSessionDataTask:(NSURLSessionDataTask *)dataTask{
    if (!self.taskArr) {
        self.taskArr = [[NSMutableArray alloc]initWithCapacity:0];
    }
    if (dataTask) {
        [self.taskArr addObject:dataTask];
    }
}


#pragma mark -导航栏按钮

/**
 @brief 通过文字设置导航条右边按钮的标题
 @param btnTitle 按钮的标题
 */
- (void)navBarRightButtonTitle:(NSString *)btnTitle
{
    [self navBarRightButtonTitle:btnTitle withBackgroudImage:nil];
}


- (void)navBarRightButtonTitle:(NSString *)btnTitle nomralTitleColor:(UIColor *)color hightTitleColor:(UIColor *)highcolor {
    [self navBarRightButtonTitle:btnTitle withBackgroudImage:nil];
    [_btnRight setTitleColor:color forState:UIControlStateNormal];
    [_btnRight setTitleColor:highcolor forState:UIControlStateHighlighted];
}


/**
 @brief 通过图片设置导航条右边按钮
 @param 按钮的背景图片
 */
- (void)navBarRightButtonWithImageName:(NSString *)imageName
{
    [self navBarRightButtonTitle:@"" withBackgroudImage:imageName];
}

- (void)navBarRightButtonTitle:(NSString *)btnTitle withBackgroudImage:(NSString *)backgroudImage
{
    _btnRight = [self navBarButtonWithTitle:btnTitle backgroundImage:backgroudImage];
    
    if (IOS7) {
        UIBarButtonItem *spaceButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        self.navigationItem.rightBarButtonItems = @[[[UIBarButtonItem alloc] initWithCustomView:_btnRight], spaceButton];
    }
    else{
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_btnRight];
    }
    
    [_btnRight addTarget:self action:@selector(rightButtonclickEvent:) forControlEvents:UIControlEventTouchUpInside];
}

- (UIButton *)navBarButtonWithTitle:(NSString *)btnTitle backgroundImage:(NSString *)imageName {
    UIFont *textFont = [UIFont systemFontOfSize:15.0];
    CGSize size = [btnTitle sizeWithFont:textFont maxSize:CGSizeMake(200, 40)];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 4, size.width + 5, size.height)];
    [btn setTitle:btnTitle forState:UIControlStateNormal];
    [btn setTitleColor:OrangeNormal forState:UIControlStateNormal];
    [btn setTitleColor:OrangeHighligted forState:UIControlStateHighlighted];
    btn.titleLabel.font = textFont;
    if (imageName) {
        [btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        btn.frame = CGRectMake(0, 4, 30, 30);
    }
    return btn;
}

- (void)navBarLeftButtonWithBackgroundImage:(NSString *)imageName {
    _btnLeft = [self navBarButtonWithTitle:nil backgroundImage:imageName];
    
    if (IOS7) {
        UIBarButtonItem *spaceButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        self.navigationItem.leftBarButtonItems = @[[[UIBarButtonItem alloc] initWithCustomView:_btnLeft], spaceButton];
    }
    else{
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_btnLeft];
    }
    
    [_btnLeft addTarget:self action:@selector(leftButtonclickEvent:) forControlEvents:UIControlEventTouchUpInside];
}

/**
 @brief 设置导航条左边按钮的image
 @param imageName 图片名称
 */
- (void)navBarLeftButtonImage:(NSString *)imageName {
    _btnLeft = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    if (imageName) {
        [_btnLeft setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    }
    if (IOS7) {
        UIBarButtonItem *spaceButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        self.navigationItem.leftBarButtonItems = @[[[UIBarButtonItem alloc] initWithCustomView:_btnLeft], spaceButton];
    }
    else{
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_btnLeft];
    }
    
    [_btnLeft addTarget:self action:@selector(leftButtonclickEvent:) forControlEvents:UIControlEventTouchUpInside];
}


/**
 @brief 导航条右边按钮的点击事件
 @param sender 按钮对象
 */
- (void)rightButtonclickEvent:(id)sender
{
    
}

- (void)leftButtonclickEvent:(id)sender {
    
    
}

- (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion
{
    [super dismissViewControllerAnimated:flag completion:completion];
    
    // 取消退出视图控制器的网络请求
    for (NSURLSessionDataTask *dataTask in self.taskArr) {
        
        if (dataTask.state == NSURLSessionTaskStateRunning || dataTask.state == NSURLSessionTaskStateSuspended) {
            [dataTask cancel];
        }
    }
}

#pragma mark - getter
- (Loadingview *)loadingView {
    if (_loadingView == nil) {
        _loadingView =  [[[NSBundle mainBundle] loadNibNamed:@"Loadingview" owner:nil options:nil] lastObject];
        BOOL isRootClass = [self isKindOfClass:[BaseRootViewCtrl class]];
        CGFloat tarBarHeight = isRootClass ? 49 : 0;
        CGFloat height = kScreenHeight - 64 - tarBarHeight;
        CGRect rect  = CGRectMake(0, 0, kScreenWidth, height);
        [_loadingView setFrame:rect];
        _loadingView.parentView = self.view;
    }
    return _loadingView;
}



@end
